package com.v1.video.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;

import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXVideoObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.util.Helper;

/**
 * 添加好友
 * 
 * @author wangfang 2016.4.14
 */
public class AddFriendActivity extends BaseActivity implements OnClickListener {

	private static final String TAG = "AddFriendActivity";
	private static final String LINKURL = "http://apps.v1.cn/disc.shtml";
	private static final String TITLE = "第一视频";
	private static final String CONTENT = "看短视频还得用第一视频！全球最热头条，最全内容分类，还有VR视频哦~条条精彩，根本停不下来！快来和我一起看吧！http://apps.v1.cn/disc.shtml 第一视频，瞬间改变你的视界！";
	private TextView mTvTitle;
	private IWXAPI mWxApi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addfriend);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mTvTitle.setText(this.getResources()
				.getString(R.string.addfriend_title));
		mWxApi = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
		mWxApi.registerApp(Constant.APP_ID);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		this.findViewById(R.id.lay_phonebook).setOnClickListener(this);
		this.findViewById(R.id.lay_sina).setOnClickListener(this);
		this.findViewById(R.id.lay_qq).setOnClickListener(this);
		this.findViewById(R.id.lay_weixin).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.id.lay_phonebook:
			Uri smsToUri = Uri.parse("smsto:");
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SENDTO);
			intent.setData(smsToUri);
			intent.putExtra("sms_body", CONTENT);
			startActivity(intent);
			break;
		case R.id.lay_sina:
			if (Helper.checkConnection(this)) {
				onKeyShare(ShareSDK.getPlatform(this, SinaWeibo.NAME));
			} else {
				showToast(getResources().getString(R.string.net_no_click));
			}
			break;
		case R.id.lay_qq:
			if (Helper.checkConnection(this)) {
				onKeyShare(ShareSDK.getPlatform(this, QQ.NAME));
			} else {
				showToast(getResources().getString(R.string.net_no_click));
			}
			break;
		case R.id.lay_weixin:
			if (Helper.checkConnection(this)) {
				if (mWxApi.isWXAppInstalled()) {
					onkeyshareWeiXin(1);
				} else {
					showToast("亲，您尚未安装微信客户端，请下载安装~");
				}
			} else {
				showToast(getResources().getString(R.string.net_no_click));
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 分享到微信0:朋友圈，1 微信好友
	 * 
	 * @param type
	 */
	private void onkeyshareWeiXin(int type) {
		WXVideoObject video = new WXVideoObject();
		video.videoUrl = LINKURL;
		WXMediaMessage msg = new WXMediaMessage(video);
		msg.title = TITLE;
		msg.description = CONTENT;
		Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		msg.thumbData = com.v1.video.util.Util.bmpToByteArray(thumb, true);
		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("video");
		req.message = msg;
		req.scene = type == 0 ? SendMessageToWX.Req.WXSceneTimeline
				: SendMessageToWX.Req.WXSceneSession;
		mWxApi.sendReq(req);
		Constant.setShare(true);
		Constant.listenerShare.init(this, null);
	}

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	/***
	 * 一键分享
	 * 
	 * @param plat
	 */
	private void onKeyShare(Platform plat) {
		ShareParams sp = new ShareParams();
		sp.setShareType(Platform.SHARE_IMAGE);
		sp.setTitle(TITLE);
		sp.setTitleUrl(LINKURL);
		sp.setText(CONTENT);
//		Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
//				R.drawable.ic_launcher);
//		// sp.setImageUrl(url); // 直接分享网络地址
//		sp.setImageData(bitmap);
		sp.setImageUrl(Constant.LOGO_ICON_URL);
		sp.setUrl(LINKURL);
		plat.setPlatformActionListener(new SharePlatformActionListener(this,
				null));
		plat.share(sp);
	}
}
